import React from 'react';
import './activity.less';


class activity extends React.Component {
    //getCategory()
    constructor(props) {
        super(props);

        this.state = {
            list: [],

        }
    }
    componentWillMount = () => {
        this.getGoodList();
    }

    subitAction = () => {
        var name = this.refs.inputDom_name.value
       
       

        var json = {
            name: name
           
        }
        this.addGoods(json);

    }
     goodsConsole=()=>{
        console.log('im a console')
     }
    getGoodList = () => {
        React.api.get('/activity/all')
            .then((res) => {
                console.log(res)
                this.setState({
                    list: this.fitArr(res.data.obj),
                    defaultValue: ''
                })
            }).catch((err) => {
                console.log(err)
            })
    }
    addGoods = (json) => {
        React.api({
            method: 'post',
            url: '/activity/add',
            params: json
        }).then((res) => {
            if (res.data.code == 200) {
                this.setState({
                    list: this.fitArr(res.data.obj),
                    defaultValue: ''
                })
            } else {
                alert(res.msg)
            }

        })
    }
    fitArr = (arr) => {
        for (var i = 0; i < arr.length; i++) {
            arr[i].isChange = 'false';
        }

        return arr
    }
    change = (id, yn) => {

        React.api({
            method: 'get',
            url: '/activity/change?id=' + id + '&yn=' + yn
        }).then((res) => {
            console.log("change")
            console.log(res)
            if (res.data.code == 200) {

                this.getGoodList();

            } else {
                alert(res.msg)
            }

        })




    }
    update = (v) => {
        console.log(v)


    }
    render() {
        return ( < div id = 'categoryView' >
                <
                div className = 'category_item' >
                <
                input ref = 'inputDom_name'
                defaultValue = {this.state.defaultValue} placeholder = '活动名字' / >
                
                <
                label onClick = {
                    () => this.subitAction()
                } > 新增 < /label> 

                <
                /div> {
                this.state.list.map((v, i) => {
                    return ( <
                        div className = 'category_item'
                        key = { i } >
                        <
                        input defaultValue = { v.name }
                        />

                        {
                            v.yn === '0' &&
                                <
                                label onClick = {
                                    () => this.change(v.id, 1)
                                } > 上架 < /label> 
                        } {
                            v.yn !== '0' &&
                                <
                                label onClick = {
                                    () => this.change(v.id, 0)
                                } > 下架 < /label> 
                        } <
                        label onClick = {
                            () => this.update(v)
                        } > 修改 < /label>  < /
                        div >
                    )
                })
            } <
            /div>
    )
}
}
export default activity